import pdfkit

class To_pdf(object):
    def __init__(self,content_url) -> None:
        self.content_url = content_url
       
    '''将网页url生成pdf文件'''
    def url_to_pdf(self,pdf_file_path):
        # 将wkhtmltopdf.exe程序绝对路径传入config对象
        
        path_wkthmltopdf = './to_pdf/wkhtmltopdf.exe'
        #配置wkthmltopdf 
        config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
        # 生成pdf文件，to_file为文件路径
        try:
            pdfkit.from_url(self.content_url,pdf_file_path,configuration=config)
        except:
            print('%s无法转存!'%self.content_url)
        else:
            file_name = pdf_file_path.split('\\')[-1]
            print('%s转存成功'%file_name)


